import { useGlobSetting } from '/@/hooks/setting';
import { downloadByUrl } from '/@/utils/file/download';
import { encryptByBase64 } from '/@/utils/cipher';

export async function handleExport(url, data) {
  const params = { ...data };
  for (const key in params) {
    if (
      Array.isArray(params[key]) &&
      params[key].length &&
      params[key][0].slice(4, 5) == '-' &&
      params[key][0].slice(7, 8) == '-'
    ) {
      params[key + '_gte'] = params[key][0];
      params[key + '_lte'] = params[key][1];
      delete params[key];
    }
  }
  const { ctxAdminPath } = useGlobSetting();
  let base64 = encryptByBase64(JSON.stringify(params));
  base64 = base64.replaceAll('+', '-');
  base64 = base64.replaceAll('/', '_');
  downloadByUrl({
    url: ctxAdminPath + url + '?data=' + base64,
    target: '_self',
  });
}
